package hn.cch.framework.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class OracleDemo {

    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@192.168.10.134:1521:orcl";
        String user = "chench";
        String password = "chench";

        Connection connection = null;
        CallableStatement callableStatement = null;

        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);

            callableStatement = connection.prepareCall("{ call sp_user(?,?) }");
            callableStatement.setString(1, "c");
            callableStatement.registerOutParameter(2,
                    oracle.jdbc.OracleTypes.CURSOR);

            ResultSet resultSet = callableStatement.executeQuery();

            while (resultSet.next()) {
                System.out.println("user:" + resultSet.getString("user_code")
                        + resultSet.getString("user_name"));
            }

        } catch (Exception e) {
            System.out.println("oracleJdbc:" + e.getMessage());
        } finally {
            try {
                if (callableStatement != null
                        && callableStatement.isClosed()) {
                    callableStatement.close();
                }

                if (connection != null
                        && connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
